home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #5 & #6 / Amiga Plus CD - 1995 - No. 5 and 6.iso / pd / netz / ipdial / deviceio.c < prev    next >
C/C++ Source or Header  |  1995-07-21  |  4KB  |  248 lines

  1. /**
  2. ***  IPDial     Script program for initializing a SLIP connection
  3. ***  Copyright  (C)   1994    Jochen Wiedmann
  4. ***
  5. ***  This program is free software; you can redistribute it and/or modify
  6. ***  it under the terms of the GNU General Public License as published by
  7. ***  the Free Software Foundation; either version 2 of the License, or
  8. ***  (at your option) any later version.
  9. ***
  10. ***  This program is distributed in the hope that it will be useful,
  11. ***  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ***  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. ***  GNU General Public License for more details.
  14. ***
  15. ***  You should have received a copy of the GNU General Public License
  16. ***  along with this program; if not, write to the Free Software
  17. ***  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. ***
  19. ***
  20. ***
  21. ***  This file implements something like a device class. It's only
  22. ***  reason is the lack of a possibility to determine the real state
  23. ***  of an IORequest. (Sent? Inactive? Whatever else?)
  24. ***
  25. ***
  26. ***  Computer: Amiga 1200                       Compiler: Dice 3.01
  27. ***
  28. ***  Author:    Jochen Wiedmann
  29. ***             Am Eisteich 9
  30. ***             72555 Metzingen
  31. ***             Germany
  32. ***
  33. ***             Phone: (+0049) 7123 / 14881
  34. ***             Internet: wiedmann@neckar-alb.de
  35. **/
  36.  
  37.  
  38.  
  39.  
  40.  
  41. /**
  42. ***  Include files
  43. **/
  44. #ifndef IPDIAL_H
  45. #include "IPDial.h"
  46. #endif
  47. #include <exec/devices.h>
  48. #include <exec/io.h>
  49.  
  50.  
  51.  
  52.  
  53.  
  54. /**
  55. ***  We use the structure below for device IO, because I don't see
  56. ***  any way to check an IORequest whether it must be aborted, before
  57. ***  using or deleting it.
  58. **/
  59. struct MyIORequest
  60. { ULONG Used;
  61.   ULONG Signal;
  62.   ULONG DeviceOpen;
  63.   struct IORequest *Req;
  64. };
  65.  
  66.  
  67.  
  68.  
  69.  
  70. /**
  71. ***  This is the WaitIO() equivalent.
  72. **/
  73. BYTE DeviceIOWait(APTR wreq)
  74.  
  75. { struct MyIORequest *req = wreq;
  76.   BYTE result;
  77.  
  78.   result = WaitIO(req->Req);
  79.   req->Used = FALSE;
  80.   return(result);
  81. }
  82.  
  83.  
  84.  
  85.  
  86.  
  87. /**
  88. ***  This is the AbortIO() equivalent.
  89. **/
  90. VOID DeviceIOAbort(APTR areq)
  91.  
  92. { struct MyIORequest *req = areq;
  93.  
  94.   if (req  &&  req->Used  &&  req->Req)
  95.   { AbortIO(req->Req);
  96.     DeviceIOWait(req);
  97.   }
  98.   /**
  99.   ***  The following is needed for reasons I don't understand
  100.   ***  really.
  101.   **/
  102.   SetSignal(0, req->Signal);
  103. }
  104.  
  105.  
  106.  
  107.  
  108.  
  109. /**
  110. ***  This is the SendIO() equivalent.
  111. **/
  112. VOID DeviceIOSend(APTR sreq, UWORD Command)
  113.  
  114. { struct MyIORequest *req = sreq;
  115.  
  116.   if (req->Used)
  117.   { fprintf(stderr, "Internal error: IORequest in use.\n");
  118.     exit(20);
  119.   }
  120.   req->Used = TRUE;
  121.   req->Req->io_Command = Command;
  122.   SendIO(req->Req);
  123. }
  124.  
  125.  
  126.  
  127.  
  128.  
  129. /**
  130. ***  This is the DoIO() equivalent.
  131. **/
  132. BYTE DeviceIODo(APTR dreq, UWORD Command)
  133.  
  134. { struct MyIORequest *req = dreq;
  135.  
  136.   if (req->Used)
  137.   { fprintf(stderr, "Internal error: IORequest in use.\n");
  138.     exit(20);
  139.   }
  140.   req->Req->io_Command = Command;
  141.   return(DoIO(req->Req));
  142. }
  143.  
  144.  
  145.  
  146.  
  147.  
  148. /**
  149. ***  This is the DeleteIORequest() equivalent.
  150. **/
  151. VOID DeviceIODelete(APTR dreq)
  152.  
  153. { struct MyIORequest *req = dreq;
  154.  
  155.   if (req)
  156.   { DeviceIOAbort(req);
  157.     if (req->DeviceOpen)
  158.     { CloseDevice(req->Req);
  159.     }
  160.     if (req->Req)
  161.     { struct MsgPort *port = req->Req->io_Message.mn_ReplyPort;
  162.  
  163.       DeleteIORequest(req->Req);
  164.       DeleteMsgPort(port);
  165.     }
  166.     free(req);
  167.   }
  168. }
  169.  
  170.  
  171.  
  172.  
  173.  
  174. /**
  175. ***  This is the CreateIORequest() equivalent.
  176. **/
  177. APTR DeviceIOCreate(ULONG Size)
  178.  
  179. { struct MyIORequest *req;
  180.  
  181.   if ((req = malloc(sizeof(*req))))
  182.   { struct MsgPort *port;
  183.  
  184.     req->Used = FALSE;
  185.     req->DeviceOpen = FALSE;
  186.     if ((port = CreateMsgPort()))
  187.     { req->Signal = (1 << port->mp_SigBit);
  188.       if ((req->Req = CreateIORequest(port, Size)))
  189.       { return(req);
  190.       }
  191.       DeleteMsgPort(port);
  192.     }
  193.     free(req);
  194.   }
  195.   return(NULL);
  196. }
  197.  
  198.  
  199.  
  200.  
  201.  
  202. /**
  203. ***  This is the OpenDevice() equivalent.
  204. **/
  205. BYTE DeviceIOOpen(STRPTR devname,
  206.           ULONG unit,
  207.           APTR oreq,
  208.           ULONG flags)
  209.  
  210. { struct MyIORequest *req = oreq;
  211.   BYTE error;
  212.  
  213.   if (req->DeviceOpen)
  214.   { fprintf(stderr, "Internal error: Device already open.\n");
  215.     exit(20);
  216.   }
  217.   if (!(error = OpenDevice(devname, unit, req->Req, flags)))
  218.   { req->DeviceOpen = TRUE;
  219.   }
  220.   return(error);
  221. }
  222.  
  223.  
  224.  
  225.  
  226.  
  227. /**
  228. ***  This function returns the IORequest attached to a struct
  229. ***  MyIORequest.
  230. **/
  231. struct IORequest *DeviceIOReq(APTR rreq)
  232.  
  233. { return(((struct MyIORequest *)rreq)->Req);
  234. }
  235.  
  236.  
  237.  
  238.  
  239.  
  240. /**
  241. ***  This function returns an IORequest's signal. (Not the
  242. ***  signal number.)
  243. **/
  244. ULONG DeviceIOSignal(APTR sreq)
  245.  
  246. { return(((struct MyIORequest *) sreq)->Signal);
  247. }
  248.